package com.xin.ruanjian.omsystem.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.activity.LoginActivity;
import com.xin.ruanjian.omsystem.app.MyApplication;
import com.xin.ruanjian.omsystem.bases.BaseFragment;
import com.xin.ruanjian.omsystem.bases.Login;
import com.xin.ruanjian.omsystem.bean.UserBean;
import com.xin.ruanjian.omsystem.utils.GlideUtils;
import com.xin.ruanjian.omsystem.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;

import static com.xin.ruanjian.omsystem.bases.Contants.USER_CENTER;

/**
 * Created by local123 on 2018/2/3.
 */

public class PersonFragment extends BaseFragment {
    @BindView(R.id.iv_user_pic)
    ImageView ivUserPic;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_phone)
    TextView tvUserPhone;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.tv_order_01)
    TextView tvOrder01;
    @BindView(R.id.tv_order_02)
    TextView tvOrder02;
    @BindView(R.id.tv_order_03)
    TextView tvOrder03;
    @BindView(R.id.tv_seat)
    TextView tvSeat;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_evaluate)
    TextView tvEvaluate;

    private Login login;


    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_person, container, false);
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData() {
        login = MyApplication.getLogin();
        showLoadingDialog();
        HttpUtils httpUtils = new HttpUtils(USER_CENTER) {
            @Override
            public void onError(Call call, Exception e, int id) {
                showErrorDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                try {
                    JSONObject o = new JSONObject(response);
                    int state = o.getInt("state");
                    if (state == 1) {
                        UserBean bean = new Gson().fromJson(response, UserBean.class);
                        tvUserName.setText(bean.getMsg().getUsername());
                        tvUserPhone.setText(bean.getMsg().getUserphone());
                        GlideUtils.glideLoader(context, bean.getMsg().getUserhead(), ivUserPic, GlideUtils.Shape.CIRCLE);
                    }else if (state == 9) {
                        Toast(R.string.token_verification_fails_please_log_in_again);
                        startActivity(new Intent(context, LoginActivity.class)
                        .putExtra("jump", true));
                        getActivity().finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        if (login != null) {
            httpUtils.addParam("userid", login.getUserId()+"")
                    .addParams("token", login.getToken());
            httpUtils.clicent();
        }

    }

}
